<?php

// 员工管理类

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Crypt;
use Session;

class AdminController extends Controller
{
    
	// 加载 权限列表
	public function authlist( $admin_id='' )
	{
		// 获取控制器
		$controllers = DB::table('node')
				-> select( DB::raw("distinct(controller)"),'desc' )
				// -> take(3)
				-> get();
		
		// 获取 全部方法
		$functions = array();
		foreach( $controllers as $key=> $info )
		{
			
			// 该控制器下的方法
			$funs = DB::table('node')
				-> where('controller',$info->controller)
				-> get();
			foreach( $funs as $k=> $funcinfo )
			{
				$fun = array();
				$fun['id'] = $funcinfo->id;
				$fun['name'] = $funcinfo->name;
				$fun['controller'] = $funcinfo->controller;
				$fun['func'] = $funcinfo->func;
				// 控制器下对应的方法
				$functions[$key][$k] = $fun;
			}
		
		}

		// 已有权限
		$auths = DB::table('node_admin')
			-> where('admin_id',$admin_id)
			-> get();
		$auth = array();
		foreach( $auths as $key => $info )
		{
			// 节点id
			$auth[] = $info -> node_id;
		}
		
		// 权限列表
		return view('admin.auth',['admin_id'=>$admin_id,'controllers'=>$controllers,'functions'=>$functions,'auth'=>$auth]);
	
	}
	
	
	// 执行修改 增加权限
	public function addauth( Request $request )
	{
		// 包含节点id 和 admin_id
		$data = $request -> except('_token');
		// 清除无效值
		unset($data['all'][0]);
		// 查看该管理员有无权限
		$history = DB::table('node_admin')
				-> where('admin_id',$data['admin_id'] )
				-> get();
		if( count($history)>0 )
		{
			// 删除旧的权限
			$history = DB::table('node_admin')
				-> where('admin_id',$data['admin_id'] )
				-> delete();
		}
		
		foreach( $data['all'] as $key => $node )
		{
			$insert = array();
			$insert['admin_id'] = $data['admin_id'] ;
			$insert['node_id'] = $node;
			// 创建
			$insert_id = DB::table('node_admin')->insertGetId($insert);
		}
		
		$this->show_msg('权限更改成功');
		
		
	}
	
	
	
	
	/*
	 *
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
	
	// 职工 列表
    public function index( Request $request )
    {
        // 开始
        // 获取路由
        $route_array = request()->route()->getAction();
        $controller = explode("\\",$route_array['controller']);
        $result = explode("@",$controller[3]);
        // 控制器 	$result[0];
        // 方法 	$result[1];
        $auth_final = '';
        foreach( Session::get('auths') as $key=> $auth_info )
        {
            // 符合条件
            if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
            {
                $auth_final = true;
                break;
            }else
            {
                $auth_final = false;
            }
        }
        // 权限验证失败
        if( !$auth_final )
        {
            return redirect('/')->with( ['authinfo'=>'没有此项权限'] );
        }
        // 结尾
		// 获取搜索内容
		$keyword = $request->input('keyword',"");
		if( empty($keyword) )
		{
			// 为空 说明没有搜索
			$one = 'a.id';
			$two = '!=';
			$three = '0';
		}else
		{
			// 是员工名
			$one = 'a.name';
			$two = 'like';
			$three = '%'.$keyword.'%';
		}
		
		// 查询所有职工 每页15条数据
		$admin = DB::table('admin as a')
				-> select( 'a.id','a.name','a.lasttime' )
				-> where( $one,$two,$three ) 
				-> orderBy( 'a.id','desc' ) 
				-> paginate(15);
		return view( 'admin.list',[ 'admin'=>$admin,'request'=>$keyword ] );
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */

    // 职工 创建 编辑 页面
    public function create( $admin_id='' )
    {
        // 开始
        // 获取路由
        $route_array = request()->route()->getAction();
        $controller = explode("\\",$route_array['controller']);
        $result = explode("@",$controller[3]);
        // 控制器 	$result[0];
        // 方法 	$result[1];
        $auth_final = '';
        foreach( Session::get('auths') as $key=> $auth_info )
        {
            // 符合条件
            if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
            {
                $auth_final = true;
                break;
            }else
            {
                $auth_final = false;
            }
        }
        // 权限验证失败
        if( !$auth_final )
        {
            return redirect('/')->with( ['authinfo'=>'没有此项权限'] );
        }
        // 结尾
		
		if( true )
		{
			// 创建
			return view( 'admin.add' );
		}else
		{
			// 编辑
			$admin_info = DB::table('admin')
					-> where('id', $admin_id)
					-> first();
			return view( 'admin.add' );
		}
		
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */



    // 执行 员工注册 编辑
    public function store( $admin_id='',Request $request )
    {
		$data = $request -> except('_token');
		
		// 名字 
		$name_length=mb_strlen( $data['name'],'utf-8' );
		if( $name_length<1 )
		{
			$this->show_msg('职工名字不能为空');
		}
		
		if( empty($data['pass']) )
		{
			$this->show_msg( '请输入登录密码' );
		}
		
		if( empty($data['repass']) )
		{
			$this->show_msg( '请输入确认密码' );
		}
		
		
		if( $data['pass']!=$data['repass'] )
		{
			$this->show_msg( '两次密码输入不一致' );
		}
		
		unset($data['repass']);
		
		if( $admin_id == '' )
		{
			$data['regtime']=date('Y-m-d H:i:s',time());
			$data['lasttime']=date('Y-m-d H:i:s',time());
			// 创建
			$insert_id = DB::table('admin')->insertGetId($data);
			if( $insert_id )
			{
				$this->show_msg( '职工创建成功' );
			}else
			{
				$this->show_msg( '操作失败' );
			}
		}else
		{
			// 编辑
			$update = DB::table('admin')
					-> where('id',$admin_id)
					-> update( $data );
			if( $update )
			{
				$this -> show_msg( '职工编辑成功' );
			}else
			{
				$this -> show_msg( '操作失败' );
			}
			
		}
    }

    public function del( Request $request ){
        $id = $request->input('id');
        $admin_info = DB::table('admin')
            -> where('id', $id)
            -> delete();
        if( $admin_info ){
            echo 1;
        }else{
            echo 2;
        }
    }
	
	

}
